﻿using Gym.Application.Common.Interfaces;
using Gym.Domain.SubscriptionAggregate;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gym.Infrastructure.Persistence.Repositories
{
    public class SubscriptionsRepository : ISubscriptionsRepository
    {
        private readonly GymDbContext _dbContext;
        public SubscriptionsRepository(GymDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        public async Task AddSubscriptionAsync(Subscription subscription)
        {
            await _dbContext.Subscriptions.AddAsync(subscription);
            await _dbContext.SaveChangesAsync();
        }

        public async Task<bool> ExistsAsync(Guid id)
        {
            return await _dbContext.Subscriptions
            .AsNoTracking()
            .AnyAsync(subscription => subscription.Id == id);
        }

        public async Task<Subscription?> GetByIdAsync(Guid id)
        {
            return await _dbContext.Subscriptions
                .FirstOrDefaultAsync(subscription => subscription.Id == id);
        }

        public async Task<List<Subscription>> ListAsync()
        {
            return await _dbContext.Subscriptions.ToListAsync();
        }

        public async Task UpdateAsync(Subscription subscription)
        {
            _dbContext.Update(subscription);
            await _dbContext.SaveChangesAsync();
        }
    }
}
